package com.example.huichaoliu;

import java.util.ArrayList;
import java.util.List;

import com.huichaoliu.adapters.ViewPagerAdapter;
import com.huichaoliu.app.R;
import com.rederxu.tools.DisplayUtils;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import android.widget.Toast;

public class FittingFragment extends Fragment implements OnClickListener{

	private View v = null;
	private View layout_type = null;
	private TextView tv_type_cloth = null;
	private TextView tv_type_pants = null;
	private TextView tv_type_shoes = null;
	private TextView tv_type_all   = null;
	private Button btn_bag = null;
	private Button btn_buy = null;
	private int [] cloth = {R.drawable.model_sy_002,R.drawable.model_sy_003,R.drawable.model_sy_005};
	private int [] pants = {R.drawable.model_kz_001,R.drawable.model_kz_002,R.drawable.model_kz_003};
	private int [] shoes = {R.drawable.model_sx_001,R.drawable.model_sx_002};
	private Gallery galleryCloth = null;
	private Gallery galleryPants = null;
	private Gallery galleryShoes = null;
	private ImageView ic_model = null;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if(v == null){
			v = inflater.inflate(R.layout.fragment_fitting, null);
		}
		ViewGroup parent = (ViewGroup) v.getParent();
		if (parent != null) {
			parent.removeView(v);
		}
		init();
		return v;
	}
	
	@SuppressWarnings("deprecation")
	private void init(){
		layout_type = v.findViewById(R.id.layout_type);
		tv_type_cloth = (TextView) v.findViewById(R.id.tv_type_cloth);
		tv_type_pants = (TextView) v.findViewById(R.id.tv_type_pants);
		tv_type_shoes = (TextView) v.findViewById(R.id.tv_type_shoes);
		tv_type_all = (TextView) v.findViewById(R.id.tv_type_all);
		btn_bag = (Button) v.findViewById(R.id.btn_bag);
		btn_buy = (Button) v.findViewById(R.id.btn_buy);
		ic_model = (ImageView) v.findViewById(R.id.ic_model);
		galleryCloth = (Gallery) v.findViewById(R.id.galleryCloth);
		galleryPants = (Gallery) v.findViewById(R.id.galleryPants);
		galleryShoes = (Gallery) v.findViewById(R.id.galleryShoes);
		btn_bag.setOnClickListener(this);
		btn_buy.setOnClickListener(this);
		v.setOnClickListener(this);
		tv_type_cloth.setOnClickListener(this);
		tv_type_pants.setOnClickListener(this);
		tv_type_shoes.setOnClickListener(this);
		tv_type_all.setOnClickListener(this);
		galleryCloth.setAdapter(new Adapter(cloth));
		galleryPants.setAdapter(new Adapter(pants,1));
		galleryShoes.setAdapter(new Adapter(shoes));
	}

	public void showTypeSelected(){
		layout_type.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void onClick(View v) {
		layout_type.setVisibility(View.GONE);
		switch(v.getId()){
		case R.id.btn_bag:
			Toast.makeText(getActivity(), "已加入购物车", 1).show();
			break;
		case R.id.btn_buy:
			startActivity(new Intent(getActivity(),EnsureOrderActivity.class));
			break;
		case R.id.tv_type_cloth:
			galleryCloth.setVisibility(View.VISIBLE);
			galleryPants.setVisibility(View.INVISIBLE);
			galleryShoes.setVisibility(View.INVISIBLE);
			break;
		case R.id.tv_type_pants:
			galleryCloth.setVisibility(View.INVISIBLE);
			galleryPants.setVisibility(View.VISIBLE);
			galleryShoes.setVisibility(View.INVISIBLE);
			break;
		case R.id.tv_type_shoes:
			galleryCloth.setVisibility(View.INVISIBLE);
			galleryPants.setVisibility(View.INVISIBLE);
			galleryShoes.setVisibility(View.VISIBLE);
			break;
		case R.id.tv_type_all:
			galleryCloth.setVisibility(View.VISIBLE);
			galleryPants.setVisibility(View.VISIBLE);
			galleryShoes.setVisibility(View.VISIBLE);
			break;
		}
	}
	
	private class Adapter extends BaseAdapter{

		private int [] data = null;
		private int type = -1;
		public Adapter(int [] data){
			this.data = data;
		}
		
		public Adapter(int [] data,int type){
			this.data = data;
			this.type = type;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(convertView == null){
				convertView = new ImageView(getActivity());
			}
			ImageView ic = (ImageView)convertView;
			ic.setImageResource(data[position]);
			return ic;
		}
		
	}
}
